<?php
class HackerU_View
{
	public function __construct()
	{
		$this->isXhr = HackerU_Http::getInstance()->isXhr();
	}
	
	
	public function __get($paramName)
	{
		$methodName = 'get'.ucfirst($paramName);
		if(method_exists($this, $methodName)){
			return $this->$methodName();
		}
		
		if(!isset($this->$paramName)){
			$this->$paramName = null;
		}
		return $this->$paramName;
	}
	
	public function __set($paramName, $paramVal)
	{
		$methodName = 'set'.ucfirst($paramName);
		if(method_exists($this, $methodName)){
			return $this->$methodName($paramName, $paramVal);
		}
		$this->$paramName = $paramVal;
	}
	
	/**
	 * 
	 * escape html
	 * @param string $data
	 */
	public function escape($data)
	{
		return htmlspecialchars($data);
	}
	
	public function includeCss(array $cssFiles = array())
	{
		
	}
	
	public function includeJs(array $jsFiles = array())
	{
		
	}
	
	/**
	 * 
	 * create url link
	 * @param array $urlData assoc etc.(controllerName, actionName ..)
	 */
	
	public function url(array $urlData = array())
	{
		$url = 'index.php?a=';
		$url .= isset($urlData['ctrl']) ? $urlData['ctrl'] : 'index';
		$url .= '/'; 
		$url .= isset($urlData['act']) ? $urlData['act'] : 'index'; 
		return $url;	
	}
	
	public function displayFormErrors()
	{
		require_once 'View/Templates/formErrors.phtml';
	}
	
	public function render($filePath = null)
	{
		if(!$filePath){
			$filePath = $this->controllerName.'/'.$this->actionName;
		}
		if(!$this->isXhr){
			require_once 'View/Layout/header.phtml';
		}
		require_once 'View/'.$filePath.'.phtml';
		if(!$this->isXhr){
			require_once 'View/Layout/footer.phtml';
		}
	}
}